package ashbyutils.servlets;

import java.io.IOException;

import javax.mail.MessagingException;
import javax.mail.internet.AddressException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import ashbyutils.email.Email;

public class EmailServlet extends HttpServlet {

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,
			IOException {

		String name = request.getParameter("name");
		String emailAddress = request.getParameter("email");
		String inquiry = request.getParameter("inquiry");

		String message = "An inquiry was submitted by " + name + "(" + emailAddress + ")<br /><br />" + inquiry;
		System.out.println("[Ashby-Solutions]");
		System.out.println(message);

		Email email = Email.createInstance();
		email.setTo("ashbygreg@yahoo.com");
		email.setFrom("ashbygreg@yahoo.com");
		email.setSubject("[Ashby-Solutions]");
		email.setBody(message);
		email.setHtml(true);

		try {
			email.send();
		} catch (AddressException e) {
			e.printStackTrace();
		} catch (MessagingException e) {
			e.printStackTrace();
		}

		StringBuffer buffer = new StringBuffer();
		buffer.append("<html><head><title>Ashby Solutions</title></head><body>");
		buffer
				.append("<div style=\"font-family: sans-serif; font-size: normal; color: gray; border-color: black; border-width: 1px;\"><img src=\"./images/ashby3.jpg\" />&nbsp;&nbsp;&nbsp;smart. pleasant. done.</div>");
		buffer.append("<br />Thanks!  Someone will get back to you shortly.</body></html>");
		response.getOutputStream().print(buffer.toString());
	}

}
